from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel
from math import sqrt

app = FastAPI()

class Numbers(BaseModel):
    a: float
    b: float

class PrimeCheck(BaseModel):
    number: int

def is_prime(n: int) -> bool:
    """判断一个数是否为素数"""
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(sqrt(n)) + 1, 2):
        if n % i == 0:
            return False
    return True


@app.post("/prime_check")
async def check_prime(data: PrimeCheck):
    """判断一个数是否为素数"""
    return {
        "number": data.number,
        "is_prime": is_prime(data.number),
        "message": f"{data.number}是素数" if is_prime(data.number) else f"{data.number}不是素数"
    }

@app.get("/resources/greeting/{name}")
async def get_greeting(name: str):
    """问候接口"""
    return {"result": f"Hello, {name}!"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)